/*
** Copyright (c) 2008 Mobile GCalendarManager
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.ui.cli.commands;

import java.util.List;

import org.apache.log4j.Logger;

import au.edu.mq.comp.itec800.mgc.lib.calendar.GEvent;
import au.edu.mq.comp.itec800.mgc.ui.cli.ICommand;
import au.edu.mq.comp.itec800.mgc.util.CommandUtils;
import au.edu.mq.comp.itec800.mgc.util.MGCUtils;

import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.EventWho;
import com.google.gdata.data.extensions.Reminder;

/**
 * Event-context management class.
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public enum EventCommand implements ICommand
{
  help
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  list
  {
    @Override
    public boolean  execute(final String ... args)
    {
      boolean                             succeeded = false;
      final GEvent                        gevt      = MGCUtils.getGAccount()
                                                      .getCalendarManager()
                                                      .getEventManager()
                                                      .getCurrentEvent();
      final CalendarEventEntry            evt   = gevt.getEvent();
      final StringBuffer                  out   = new StringBuffer(512);
      List<Reminder>                      rems  = null;
      List<EventWho>                      whos  = null;
      int                                 idx   = 0;

      rems  = evt.getReminder();
      whos  = evt.getParticipants();
      out.append(">> Reminders:\n");
      if (rems.size() > 0)
      {
        idx = 0;
        for (Reminder r : rems)
        {
          out.append(String.format("\t %2d. %s %d\n",
                                   idx++, r.getMethod(), r.getMinutes()));
        }
      }
      else
        out.append("\t None.\n");
      out.append(">> Guests:\n");
      if (whos.size() > 0)
      {
        idx = 0;
        for (EventWho w : whos)
        {
          final String  status  = w.getAttendeeStatus();

          out.append(String.format("\t %2d. %s %s\n",
                                   idx++, w.getEmail(),
                                   status == null ? "accepted" : status.substring(status.lastIndexOf('.') + 1)));
        }
      }
      else
        out.append("\t None.\n");
      MGCUtils.getConsole().getIO().output(out.toString());
      return (succeeded);
    }
  },
  noop
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (true);  //  does nothing but does it well
    }
  };

  public boolean  execute(final String ... args)
  {
    return (false);
  }

  public boolean  needsAuthentication()
  {
    return (false);
  }

  public ICommand         getMaster()
  {
    return (null);
  }

  public boolean          isAlias()
  {
    return (false);
  }

  public ICommand         getTopMaster()
  {
    return (CommandUtils.getTopMaster(this));
  }

  public String   help()
  {
    return ("");
  }

  private static final Logger logger    = Logger.getLogger(GuestCommand.class);
}
